package stu.cn.ua.rgr;

import process.Actor;
import process.DispatcherFinishException;
import process.IWaitCondition;
import queues.QueueForTransactions;
import widgets.ChooseRandom;

public class Pogruzchik extends Actor {
	
	private Main gui;
	private Model model;
	private QueueForTransactions queueOfGruzovikDownload;
	private double finishTime;
	private ChooseRandom rnd;
	
	public Pogruzchik(String name, Main gui, Model model) {
		this.setNameForProtocol(name);
		this.finishTime=Double.parseDouble(gui.getJTextTime().getText());
		this.queueOfGruzovikDownload = model.getQueueOfGruzovikDownload();
		this.gui = gui;
		this.model = model;
		this.rnd = gui.getChooseRandomGrDownload();
	

	}
	@Override
	protected void rule() {
		IWaitCondition queueSizeGt0 = new IWaitCondition() {
			
			@Override
			public boolean testCondition() {
				
				return queueOfGruzovikDownload.size()>0;
			}
			public String toString() {
				
			return " чекає вантажівку на загрузку";	
			}
			
		};
		while (getDispatcher().getCurrentTime()<finishTime) {
			try {
				waitForCondition(queueSizeGt0);
			} catch (DispatcherFinishException e) {
				
				return;
			}
			Gruzovik g = (Gruzovik) queueOfGruzovikDownload.removeFirst();
			holdForTime(rnd.next());
			g.setLoad();
			getDispatcher().printToProtocol(g+" наповнена");
		}
			
		}

	}


